package ysq.xyg.base.api.aliapi;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;

/**
 * 短信Api
 *
 * @author shengQiang yu
 * @date 2019-3-28 16:42:11
 */
public class AliSmsApi {
    // 您的验证码${code}，该验证码${m}分钟内有效，请勿泄漏于他人！
    public static final String TEMPLATE_CODE = "SMS_182415448";

    public static final String TEMPLATE_CODE_TWO = "SMS_185210224";

    //配置您申请的KEY
    public static final String APPKEY ="LTAI4Fg1WsHDtdY9RT2aj16z";
    public static final String APPSECRET ="t0My6VehEGCOZkjLCyo7lVOpjM2Cwz";

    public static ReturnEntity send(String phone, String templateCode, String templateParam){
        DefaultProfile profile = DefaultProfile.getProfile("default", APPKEY, APPSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "上业新房通");
        request.putQueryParameter("TemplateCode", templateCode);
        // TemplateParam格式为
        request.putQueryParameter("TemplateParam", templateParam);

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getHttpStatus());
            if (response.getHttpStatus() == 200) {
                return new ReturnEntity();
            } else {
                return new ReturnEntity(BaseConstant.FAIL, response.getData());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new ReturnEntity(BaseConstant.FAIL, "发送失败");
    }
}
